package com.summer.First.test;

import java.util.Arrays;
import java.util.Collections;

public class testSeMid {
    public static void main(String[] args) {
        int[] arr1={1};
        //升序
        Arrays.sort(arr1);
        //降序
        //Arrays.sort(arr1, Collections.reverseOrder());
        System.out.println("升序排列数组:"+Arrays.toString(arr1));
        int t=mid(arr1,1);
        System.out.println("第一次出现该数字的下标:"+t);
        int total=toalNum(arr1,t);
        System.out.println("该数字共有~"+total+"~个");
    }
    public static int mid(int[] a,int b){
        int target=-1;
        int left=0;
        int right=a.length-1;
        while (left<=right&&right>=0&&left<a.length){
            int mid=left+(right-left)/2;
            if(a[mid]==b){
                while (mid-1>=0&&a[mid]==a[mid-1]){
                    mid--;
                }
                target=mid;
                break;
            }else if (a[mid]>b){
                right=mid-1;

            }else {
                left=mid+1;
            }
        }
        return  target;
    }
    public static int toalNum(int[] a,int x){
        if (x<0){
            return 0;
        }else {
            int total=1;
            for (int i=x+1;i<a.length;i++){
                if (a[i]==a[x]){
                    total++;
                }else {
                    break;
                }
            }
            return total;
        }

    }
}
